# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test01
# Description:    例13.1
# Author:       ASUS
# Date:         2019/4/13
#-------------------------------------------------------------------------------


#以写模式打开文件
f = open('./files/test1.txt','w')
print(f.write('I Love '))
print(f.write('python'))
f.close()

#以读模式打开文件
f = open('./files/test1.txt','r')
print(f.read(7))
print(f.read(6))
f.close()

#如果文件不存在，会抛出异常
try:
    f = open('./files/test2.txt','r+')
except Exception as e:
    print(e)

#用追加可读写模式打开文件
f = open('./files/test2.txt','a+')
print(f.write('hello'))
f.close()

f = open('./files/test2.txt','a+')
print(f.read())
f.seek(0)
print(f.read())
f.close()

#用可读写方式打开文件,该文件内容会被清空
try:
    f = open('./files/test2.txt','w+')
    print(f.read())
    f.write('How are you')
    f.seek(0)
    print(f.read())
finally:
    f.close()




